<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/2 0002
 * Time: 15:16
 */

namespace app\admin\controller;


use app\model\model\News_type;
use app\model\model\Reply;
use think\Request;

class Blog extends Base
{
    protected $_News;
    protected $_News_type;
    protected $_Label;
    protected $_Message;
    protected $_reply;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_News = new \app\model\model\News();
        $this->_News_type = new News_type();
        $this->_Label = new \app\model\model\Label();
        $this->_Message = new \app\model\model\Message();
        $this->_reply = new Reply();
    }
    /*=======================博客分类===========================*/
    public function typelists(){
        return $this->fetch();
    }
    public function getListType(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $where['classify'] = 2;
        $data = $this->_News_type->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);

        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function typeadd(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{
            $data = [
                'title'=>input('title'),
                'keyword'=>input('keyword'),
                'description'=>input('description'),
                'name'=>input('name'),
                'content'=>input('editorValue'),
                'create_time'=>time(),
                'classify'=>2,
            ];
            $re = $this->_News_type->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function typeup(){
        if(!Request::instance()->post()){
            $data = $this->_News_type->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'title'=>input('title'),
                'keyword'=>input('keyword'),
                'description'=>input('description'),
                'name'=>input('name'),
                'content'=>input('editorValue'),
            ];
            $re = $this->_News_type->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOneType(){
        $data = $this->_News_type->getOne(input('id'));

        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function delType(){

        $re = $this->_News_type->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAllType(){
        $re = $this->_News_type->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function typesort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_News_type->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /*=======================博客==========================*/
    public function lists(){
        $data = $this->_News_type->getListSelect(['classify'=>2]);
        $this->assign('type',$data);
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['n.name'] = ['like',"%$name%"];
        }
        $type = input('type');
        if($type){
            $where['n.type_id'] = ['like',"%$type%"];
        }
        $where['n.flag'] = ['NEQ',3];
        $where['n.classify'] = 2;
        $data = $this->_News->getList($where,input('limit'));
        foreach ($data as $v){
            $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['pinglun'] = '查看评论';
            $v['pl'] = $this->_Message->getcount(['blog_id'=>$v['id']]);
            $v['uname'] = str_replace(" ","-",$v['name']);
            if($v['flag']==1){
                $v['flag']='已上架';
            }else{
                $v['flag']='未上架';
            }
        }

        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            $label = $this->_Label->getListSelect();
            $this->assign('tags',$label);
            $data = $this->_News_type->getListSelect(['classify'=>2]);
            $this->assign('type',$data);
            return $this->fetch();
        }else{

            $data = [
                'name'=>input('name'),
                'author'=>input('author'),
                'intro'=>input('intro'),
                'img_url'=>input('img_url'),
                'content'=>input('editorValue'),
                'tags'=>input('tags'),
                'type_id'=>input('type_id'),
                'title'=>input('title'),
                'keyword'=>input('keyword'),
                'description'=>input('description'),
                'pv'=>input('pv'),
                'create_time'=>time(),
                'classify'=>2,
                'alt'=>input('alt')
            ];
            $re = $this->_News->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $label = $this->_Label->getListSelect();
            $this->assign('tags',$label);
            $data1 = $this->_News_type->getListSelect(['classify'=>2]);
            $this->assign('type',$data1);
            $data = $this->_News->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'author'=>input('author'),
                'intro'=>input('intro'),
                'content'=>input('editorValue'),
                'tags'=>input('tags'),
                'type_id'=>input('type_id'),
                'title'=>input('title'),
                'keyword'=>input('keyword'),
                'description'=>input('description'),
                'pv'=>input('pv'),
                'create_time'=>time(),
                'alt'=>input('alt')
            ];
            if(input('img_url')){
                $data['img_url'] = input('img_url');
            }

            $re = $this->_News->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_News->getOne(input('id'));
        $data['img_url']='<img style="height: 50px;width: 80px;" src="'.$data['img_url'].'">';
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
        $data = ['flag'=>3];
        $re = $this->_News->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
        $data = ['flag'=>3];
        $re = $this->_News->upAll(input()['data'],$data);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 排序
     * */
    public function sort(){
        $data = ['sort'=>input('sort')];
        $re = $this->_News->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /**
     * 排序
     * */
    public function shangjia(){
        $data = ['flag'=>input('flag')];
        $re = $this->_News->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    /*==========================回收站==============================================*/
    /**
     * 回收站页面
     * */
    public function recycled(){
        $data = $this->_News_type->getListSelect(['classify'=>2]);
        $this->assign('type',$data);
        return $this->fetch();
    }
    /**
     * 回收站列表数据
     * */
    public function getRecycledList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['n.name'] = ['like',"%$name%"];
        }
        $type = input('type');
        if($type){
            $where['n.type_id'] = ['like',"%$type%"];
        }
        $where['n.flag'] = 3;
        $where['n.classify'] = 2;
        $data = $this->_News->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 完全删除
     * */
    public function realDell(){
        $re = $this->_News->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量完全删除
     * */
    public function realDellAll(){
        $re = $this->_News->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 恢复
     * */
    public function regain(){
        $data = ['flag'=>1];
        $re = $this->_News->up(['id'=>input('id')],$data);
        if($re){
            return json_echo_msg(200,'恢复成功！');
        }else{
            return json_echo_msg(400,'恢复失败！');
        }
    }
    /**
     * 批量恢复
     * */
    public function regainAll(){
        $data = ['flag'=>1];
        $re = $this->_News->upAll(input()['data'],$data);
        if($re){
            return json_echo_msg(200,'恢复成功！');
        }else{
            return json_echo_msg(400,'恢复失败！');
        }
    }
    /*============评论===============*/
    public function message(){
        $id = input('id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    public function getmessage(){
        $where = ['blog_id'=>input('id')];
        $data = $this->_Message->getList($where,input('limit'));
        foreach ($data as $v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            if($v['status']==1){
                $v['status'] = '未审核';
            }else{
                $v['status'] = '已通过';
            }
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    public function messageDell(){
        $re = $this->_Message->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    public function shenhe(){
        $re = $this->_Message->up(['id'=>input('id')],['status'=>2]);
        if($re){
            return json_echo_msg(200,'成功！');
        }else{
            return json_echo_msg(400,'失败！');
        }
    }
    public function huifu(){
        if(!Request::instance()->post()){
            $data = $this->_Message->getOne(input('id'));
            $data2 = $this->_reply->getOne2(['m_id'=>input('id')]);
            $this->assign('data2',$data2);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            if(!input('id')){
                $data = [
                    'm_id'=>input('mid'),
                    'name'=>input('name'),
                    'content'=>input('content'),
                    'img_url'=>input('img_url')
                ];
                $re = $this->_reply->add($data);
                if($re){
                    return json_echo_msg(200,'回复成功！');
                }else{
                    return json_echo_msg(400,'回复失败！');
                }
            }else{
                $data = [
                    'm_id'=>input('mid'),
                    'name'=>input('name'),
                    'content'=>input('content'),
                ];
                if(input('img_url')){
                    $data['img_url'] = input('img_url');
                }
                $re = $this->_reply->up(['id'=>input('id')],$data);
                if($re){
                    return json_echo_msg(200,'回复成功！');
                }else{
                    return json_echo_msg(400,'回复失败！');
                }
            }
        }
    }
}